package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.UIDUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AlpContractDetailMapper;
import com.ruoyi.system.domain.AlpContractDetail;
import com.ruoyi.system.service.IAlpContractDetailService;
import com.ruoyi.common.core.text.Convert;

/**
 * 合同表管理Service业务层处理
 * 
 * @author zhangjun
 * @date 2020-02-13
 */
@Service
public class AlpContractDetailServiceImpl implements IAlpContractDetailService 
{
    @Autowired
    private AlpContractDetailMapper alpContractDetailMapper;

    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询合同表管理
     * 
     * @param contractId 合同表管理ID
     * @return 合同表管理
     */
    @Override
    public AlpContractDetail selectAlpContractDetailById(Long contractId)
    {
        return alpContractDetailMapper.selectAlpContractDetailById(contractId);
    }

    /**
     * 查询合同表管理
     *
     * @param stageId 阶段ID
     * @return 合同表管理
     */
    @Override
    public AlpContractDetail selectAlpContractByStageId(Long stageId)
    {
        return alpContractDetailMapper.selectAlpContractByStageId(stageId);
    }


    /**
     * 查询合同表管理列表
     * 
     * @param alpContractDetail 合同表管理
     * @return 合同表管理
     */
    @Override
    @DataScope(deptAlias = "con", userAlias = "con")
    public List<AlpContractDetail> selectAlpContractDetailList(AlpContractDetail alpContractDetail)
    {
        return alpContractDetailMapper.selectAlpContractDetailList(alpContractDetail);
    }

    /**
     * 新增合同表管理
     * 
     * @param alpContractDetail 合同表管理
     * @return 结果
     */
    @Override
    public int insertAlpContractDetail(AlpContractDetail alpContractDetail)
    {
        if ("0".equals(alpContractDetail.getDelFlag())
                && null == alpContractDetail.getUserId()
                && null == alpContractDetail.getXsPrincipalId()) {
            alpContractDetail.setDelFlag("0");
            alpContractDetail.setUserId(ShiroUtils.getSysUser().getUserId());
            alpContractDetail.setXsPrincipalId(ShiroUtils.getSysUser().getUserId().toString());
            SysUser user = userService.selectUserById(ShiroUtils.getSysUser().getUserId());
            if (null != user) {
                alpContractDetail.setXsPrincipalName(user.getLoginName());
                alpContractDetail.setDeptId(user.getDeptId());
                SysDept dept = deptService.selectDeptById(user.getDeptId());
                alpContractDetail.setXsDepartment(dept.getDeptName());
            }
        }

        return alpContractDetailMapper.insertAlpContractDetail(alpContractDetail);
    }

    /**
     * 修改合同表管理
     * 
     * @param alpContractDetail 合同表管理
     * @return 结果
     */
    @Override
    public int updateAlpContractDetail(AlpContractDetail alpContractDetail)
    {
        return alpContractDetailMapper.updateAlpContractDetail(alpContractDetail);
    }

    /**
     * 删除合同表管理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAlpContractDetailByIds(String ids)
    {
        return alpContractDetailMapper.deleteAlpContractDetailByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除合同表管理信息
     * 
     * @param contractId 合同表管理ID
     * @return 结果
     */
    @Override
    public int deleteAlpContractDetailById(Long contractId)
    {
        return alpContractDetailMapper.deleteAlpContractDetailById(contractId);
    }

    @Override
    public int getCount(AlpContractDetail alpContractDetail) {
        return alpContractDetailMapper.getCount(alpContractDetail);
    }
}
